

	*****************************************************************************
	** Paper: "How election dynamics shape perceptions of electoral integrity" **
	** Authors: Richard W. Frank (richard.frank@anu.edu.au) 				   **
	**			Ferran Martinez i Coma (f.martinezicoma@griffith.edu.au)	   **
	** Journal: Electoral Studies 48										   **
	** Year: 2017															   **
	*****************************************************************************
	
	clear
	version 13
	set seed 1234
	set scheme s1mono
	graph set window fontface "Times New Roman"
	set more off
	
	** set your current directory **
	cd ""
	
	use "Frank and Martinez i Coma 2017 replication.dta"
	
	global expertlevel "expertdomestic familiarity leftrightscale  agegroup sex"
	global countrylevel "lngdppc lnpop durable inci  election_count"
	global options ", cluster(ccode)"	
	global table = "stats(coef se) e(ll chi2 p)" 
	global tablename1 = "eip1"
		
	** Figure 1: Distribution of electoral ratings **
	
		histogram rating, discrete frequency gap(30) addlabel ytitle(Frequency) ///
		ylabel(0(50)300, angle(horizontal)) ///
		xtitle(Election integrity rating (very poor [1] to very good [10])) ///
		xlabel(1(1)10)
		
		
								****************
								*** TABLE 3 ****
								****************
	
	*************
	** MODEL 1 **
	*************
		
		meologit rating ///
			$countrylevel  $expertlevel  || ccode: || election:
			estimates store M1
			outreg2 using  model1 , word replace $table		
			estat ic /* for AIC */

	*************				
	** MODEL 2 **	
	*************
			
		meologit rating ///
			fairaccess ///
			favoredincumbent ///
			violence ///
			impartial ///
			$expertlevel  $countrylevel || ccode: || election:
			estimates store M2
			outreg2 using  model2 , word replace $table	
			estat ic /* for AIC */
		
		** Table 4: Marginal change in predicted probabilites of rating scores **
		
			mchange fairaccess favoredincumbent violence impartial, amount(marg) 
			
		** Figure 4: Marginal change in predicted probabilites of rating scores **
		
			mchangeplot favoredincumbent fairaccess violence impartial, amount(marg) ///
						symbols(1 2 3 4 5 6 7 8 9 10)
					
		** test of the  proportionality of the odds across response categories **
					
		omodel logit rating ///
			fairaccess ///
			favoredincumbent ///
			violence ///
			impartial ///
			$expertlevel  $countrylevel
					
		** Brant test of the parallel regression assumption **
					
		ologit rating ///
			fairaccess ///
			favoredincumbent ///
			violence ///
			impartial ///
			$expertlevel  $countrylevel
			brant
			
		** Checking VIF scores **
	
		reg rating ///
			fairaccess ///
			favoredincumbent ///
			violence ///
			impartial ///
			$expertlevel  $countrylevel 
			
		save "Frank and Martinez i Coma temp.dta"	, replace
			
						*****************************************
						*** TABLE 5: Anchoring vignette model ***
						*****************************************			
				
		** exporting to get rid of mi set issues from the EIP data that prevent reshaping data **
		
		export excel using "/rating export.xls", firstrow(variables) replace
		clear
		import excel using "rating export.xls", sheet("Sheet1") firstrow 
		rename country Country
		drop ccode
		run "COW/COW MASTER MERGE LIST.do" /* adding updated country codes, 
		* do file available if you email Richard */

		order expert election ccode Country
		rename Country country

		sort expert

		rename vignetteA rating1
		rename vignetteB rating2
		rename vignetteC rating3
		rename rating rating4

		reshape long rating, i(expert election) j(item)

		drop if rating==.
		tab item, gen(i)
		
		rename i4 self
		
		keep expert election item rating i1 i2 i3 self
		
		/** merge back in the expert-level data **/
		
		merge m:1 expert election using "Frank and Martinez i Coma temp.dta"
		keep if _merge==3
		
		for var fairaccess favoredincumbent violence faircount impartial 		///
				expertdomestic familiarity leftrightscale agegroup sex lngdppc 	///
				lnpop durable inci  election_count 								///
				: gen r_X=self*X
			
		gen vign = 1 - self
		eq het: vign self
		constraint def 1 [lns1]self=0
		
		eq thresh: favoredincumbent fairaccess  violence impartial 				///
				expertdomestic familiarity leftrightscale  agegroup sex lngdppc ///
				lnpop durable inci  election_count  
	
	****************
	***	MODEL 3	 ***
	****************	
	
		**turning election into a numerical variable **
		encode election, generate(election2)

		gllamm rating r_fairaccess r_favoredincumbent r_violence r_impartial 				///
				r_expertdomestic r_familiarity r_leftrightscale  r_agegroup r_sex r_lngdppc ///
				r_lnpop r_durable r_inci  r_election_count i1 i2 i3 						///
				, i(expert election2 ccode) link(soprobit) s(het) constr(1) ethresh(thresh) init 
		
 		export delimited using "FMiC_2017.csv", replace

		clear
		erase "Frank and Martinez i Coma temp.dta"
		
						*****************************************
						*** TABLE 6: Extreme Bounds Analysis  ***
						*****************************************			
				
	
	** The EBA models were run in R. 
	** To replicate our findings, please run the following commands in R. 
 	
	install.packages("ExtremeBounds")
	install.packages("MASS")
	install.packages("Hmisc")
	library(foreign)
	library(ExtremeBounds)

	FMiC.2017 <- read.csv("FMiC_2017.csv")
	View(FMiC.2017)

	rating.eba <- eba(data=FMiC.2017, y = "rating", 
					  free = c("expertdomestic", "familiarity", "leftrightscale" , "agegroup", "sex" , "lngdppc", "lnpop", "durable", "inci"), 
					  doubtful = c("lawsunfair" , "favoredincumbent", "citizens", "fairaccess", "managed", "votinginfo", "fairofficials" , 
								   "legalelections" , "managed" ,"votinginfo" , "fairofficials" , "legalelections", "reglisted",  "reginaccurate" , "ineligible", "oppprevent",
								   "womenopp", "minorityopp", "leaderselect", "rallies" , "newspapers", "tv", "fairaccess", "faircoverage", "socialmedia" , "subsidies" ,
								   "donations", "accounts", "rich", "resources", "violence" , "fraudulent", "easy",  "choice", "postal" , "disabled" , "expats" , "internet",    
								   "secure", "delay", "faircount",  "intlmonitors", "domesticmonitors", "challenged", "protestspeace", "protestsviolent", "disputes", "impartial" , "info",
								   "scrutiny"), vif = 7, k= 0:3)
								   
	print(rating.eba)
	hist(rating.eba)
	hist(rating.eba, variables = c("lawsunfair" , "favoredincumbent", "citizens", "fairaccess", "managed", "votinginfo", "fairofficials" , 
								   "legalelections" , "managed" ,"votinginfo" , "fairofficials" , "legalelections", "reglisted",  "reginaccurate" , "ineligible", "oppprevent",
								   "womenopp", "minorityopp", "leaderselect", "rallies" , "newspapers", "tv", "fairaccess", "faircoverage", "socialmedia" , "subsidies" ,
								   "donations", "accounts", "rich", "resources", "violence" , "fraudulent", "easy",  "choice", "postal" , "disabled" , "expats" , "internet",    
								   "secure", "delay", "faircount",  "intlmonitors", "domesticmonitors", "challenged", "protestspeace", "protestsviolent", "disputes", "impartial" , "info",
								   "scrutiny"), mu.visible=TRUE, normal.show=TRUE, normal.lwd=1)	
				
	
	

